/*
 * smart-designs v1.0.6
 * 13-5-2024
*/

"use strict";
var Premium = Premium || {};

Premium.creative = {
    init: function() {
        /* START OF CUSTOM JS */

        gsap.set(".jpt-kv-image", { xPercent: -50, yPercent: -50 });

        Premium.communicator.init(document.body.id !== "body_expanded" ? 4 : undefined);

        Premium.animation.delay = document.body.id === "body_top" ? 1 : .5;
        var entranceAnimations = Premium.animation.getEntranceAnimation();
        Premium.communicator.api.receiveMessage(function(mess) {
            if (mess.action === "start-entrance-animation") {
                entranceAnimations.play();
            }
        });
        Premium.communicator.api.sendMessage({ action: "start-entrance-animation" });

        var bgVideo = document.querySelector(".jpt-bg video");
        if (bgVideo) {
            Premium.template.bgVideoLoop(bgVideo);
        }

        var videoMouseSense = document.querySelector(".mouse-sense");
        if (videoMouseSense) {
            var sendEvent = function(e) {
                var frameRect = window.frameElement.getBoundingClientRect();
                var mouseX = e.clientX + frameRect.left;
                var mouseY = e.clientY + frameRect.top;
                Premium.communicator.api.sendMessage({ action: "pass-on-event", event: e, mouseX: mouseX, mouseY: mouseY });
            }
            videoMouseSense.addEventListener("mousemove", function(e) {
                sendEvent(e);
            })
            videoMouseSense.addEventListener('click', function(e) {
                sendEvent(e);
            })
        }

        switch (document.body.id) {
            case "body_top":

                Premium.creative.createVideoPlayerAppended = function(video) {
                    Premium.video.sync(video, 1);
                    Premium.video.switchOnScroll(video.parentElement, 2);
                    PremiumJpControls.callOnClickFullScreen(function() {
                        video.muted = false;
                        Premium.expand.expand("expanded_video.html", "width:100%;height:100%;")
                    });
                    var prom = video.play();
                    if (prom) {
                        prom.catch(function() {})
                    }
                }

                break;

            case "body_left":
            case "body_right":

                var visualOnScroll = document.querySelector(".jpt-visual-onscroll");
                var insertWhenScrolled = document.querySelector(".jpt-insert-when-scrolled");

                Premium.creative.createVideoPlayerAppended = function(video, container) {
                    video.volume = 0;
                    PremiumJpControls.callOnClickFullScreen(function() {
                        video.muted = false;
                        Premium.expand.expand("expanded_video.html", "width:100%;height:100%;")
                    });
                    Premium.video.sync(video, 1);

                    Premium.video.switchOnScroll(document.querySelector(".jpt-video-container"), 3);
                    var prom = video.play();
                    if (prom) {
                        prom.catch(function() {})
                    }
                }

                gsap.set(".jpt-insert-when-scrolled", { marginBottom: Premium.template.getScrolledStateFlexMargin(), autoAlpha: 0 });
                if (insertWhenScrolled) {
                    gsap.set(".jpt-footer", { height: "20%" });
                }

                Premium.video.switchOnScroll(undefined, 3, function(pos) {
                    if (visualOnScroll) {
                        if (pos === "down") {
                            gsap.timeline()
                                .to(".jpt-visual-onload", { duration: .4, autoAlpha: 0 }, 0)
                                .to(".jpt-visual-onscroll", { duration: .4, autoAlpha: 1, pointerEvents: "auto", onUpdate: PremiumJpControls.resizeAll }, 0)
                        } else {
                            gsap.timeline()
                                .to(".jpt-visual-onscroll", { duration: .4, autoAlpha: 0, pointerEvents: "none" }, 0)
                                .to(".jpt-visual-onload", { duration: .4, autoAlpha: 1 }, 0)
                        }
                    }
                    if (insertWhenScrolled){
                        if (pos === "down") {
                            gsap.to(".jpt-insert-when-scrolled", .2, { marginBottom: "0", autoAlpha: 1 });
                            gsap.to(".jpt-footer", .2, { height: "15%" });
                        } else {
                            gsap.timeline().to(".jpt-insert-when-scrolled", .2, { autoAlpha: 0 })
                                .to(".jpt-insert-when-scrolled", .2, { marginBottom: Premium.template.getScrolledStateFlexMargin() }, "-=.17");
                            gsap.to(".jpt-footer", .2, { height: "20%" });
                        }
                    } 
                });

                if (document.body.id === "body_left"){
                    // left panel-specific code here

                    // Pseudo Panels
                    const sendResize = ()=>{
                        Premium.communicator.api.sendMessage({action:"resize"});
                    }
                    window.addEventListener("resize", sendResize);
                    window.addEventListener("load", sendResize)

                } else if (document.body.id === "body_right"){
                    // right panel-specific code here
                }

                break;


            case "body_back":
                // back panel code here

                var visualOnScroll = document.querySelector(".jpt-visual-onscroll");
                var insertWhenScrolled = document.querySelector(".jpt-insert-when-scrolled");

                Premium.creative.createVideoPlayerAppended = function(video, container) {
                    video.volume = 0;
                    PremiumJpControls.callOnClickFullScreen(function() {
                        video.muted = false;
                        Premium.expand.expand("expanded_video.html", "width:100%;height:100%;")
                    });
                    Premium.video.sync(video, 1);

                    Premium.video.switchOnScroll(document.querySelector(".jpt-video-container"), 3);
                    var prom = video.play();
                    if (prom) {
                        prom.catch(function() {})
                    }
                }

                bgVideo.volume = 0;
                Premium.video.sync(bgVideo, 1)

                gsap.set(".jpt-insert-when-scrolled", { marginBottom: Premium.template.getScrolledStateFlexMargin(), autoAlpha: 0 });
                if (insertWhenScrolled) {
                    gsap.set(".jpt-footer", { height: "20%" });
                }

                Premium.video.switchOnScroll(undefined, 3, function(pos) {
                    if (visualOnScroll) {
                        if (pos === "down") {
                            gsap.timeline()
                                .to(".jpt-visual-onload", { duration: .4, autoAlpha: 0 }, 0)
                                .to(".jpt-visual-onscroll", { duration: .4, autoAlpha: 1, pointerEvents: "auto", onUpdate: PremiumJpControls.resizeAll }, 0)
                        } else {
                            gsap.timeline()
                                .to(".jpt-visual-onscroll", { duration: .4, autoAlpha: 0, pointerEvents: "none" }, 0)
                                .to(".jpt-visual-onload", { duration: .4, autoAlpha: 1 }, 0)
                        }
                    }
                    if (insertWhenScrolled){
                        if (pos === "down") {
                            gsap.to(".jpt-insert-when-scrolled", .2, { marginBottom: "0", autoAlpha: 1 });
                            gsap.to(".jpt-footer", .2, { height: "15%" });
                        } else {
                            gsap.timeline().to(".jpt-insert-when-scrolled", .2, { autoAlpha: 0 })
                                .to(".jpt-insert-when-scrolled", .2, { marginBottom: Premium.template.getScrolledStateFlexMargin() }, "-=.17");
                            gsap.to(".jpt-footer", .2, { height: "20%" });
                        }
                    } 
                });


                Premium.communicator.api.receiveMessage(function(mess) {
                    if (mess.action === "pass-on-event") {
                        var element = document.elementFromPoint(mess.mouseX, mess.mouseY);
                        if (!element) return;
                        var newEvtDetail = {};
                        for (var k in mess.event) {
                            newEvtDetail[k] = mess.event[k];
                        }
                        newEvtDetail.clientX = mess.mouseX;
                        newEvtDetail.clientY = mess.mouseY;
                        newEvtDetail.deltaX = mess.event.deltaX;
                        newEvtDetail.deltaY = mess.event.deltaY;
                        if (mess.event.type === "click" && element.classList.contains("jp-controls-fullscreenbutton")) {
                            Premium.expand.expand("expanded_video.html", "width:100%;height:100%;")
                        }
                        var newEvt = new MouseEvent(mess.event.type, newEvtDetail);
                        element.dispatchEvent(newEvt);
                    }
                })

                // Pseudo Panels
                Premium.communicator.api.receiveMessage(function(mess) {
                    if (mess.action === "resize") {
                        var siteWidth = Premium.product.creativeMainEl().clientWidth;
                        var oneSide = (window.innerWidth - siteWidth) / 2;
                        gsap.set(".pseudo-top", { width: siteWidth, height: Premium.product.creativeMainEl().clientHeight, left: oneSide });
                        gsap.set(".pseudo-left, .pseudo-right", { width: oneSide });
                    }
                })
                Premium.communicator.api.sendMessage({action:"resize"});

                break;

            case "body_expanded":

                if (document.querySelector(".jpt-section-video")) {
                    Premium.creative.createVideoPlayerAppended = function(video) {
                        video.volume = 0;
                        Premium.video.sync(video, undefined, Premium.video.SyncType_Get);
                        video.addEventListener("playing", function() {
                            // setTimeout(function(){
                            document.body.style.display = "block";
                            document.body.style.opacity = 1;
                            // }, 200)  
                        }, { once: true })
                        var prom = video.play();
                        if (prom) {
                            prom.catch(function() {})
                        }
                        // Safari fix
                        document.body.addEventListener("click", function(e) {
                            if (e.target.className && (e.target.className.indexOf("jp-controls-play") > -1 || e.target.className.indexOf("jp-controls-bigplay") > -1)) {
                                if (!video.paused) {
                                    setTimeout(function() {
                                        video.play();
                                    }, 800)
                                }
                            }
                        });
                    }
                } else {
                    document.body.style.display = "block";
                    document.body.style.opacity = 1;
                }

                break;
        }

        /* END OF CUSTOM JS */
    },
    loaded: function() {
        document.body.style.opacity = 1;
    }
}